package cn.his.medical.phismange.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import java.util.LinkedList;

/**
 * @类名 CustomView
 * @描述  模仿考试  自定义滑动控件
 * @作者 xiewei
 * @创建时间 2018/4/17 10:28
 */
public class CustomView  extends LinearLayout{
    private ViewPager viewPager;
    private static int MAX_ZHi = 100000;
    private ViewAdapter viewAdapter;
    public CustomView(Context context) {
        super(context);
    }

    public CustomView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
        initDate();
    }

    private void initDate() {
        //设置适配器
        viewAdapter = new ViewAdapter(viewPager);
        viewPager.setAdapter(viewAdapter);
    }
    private void initView(Context context) {
        viewPager = new ViewPager(context);
        this.addView(viewPager);
    }

    /**
     * 设置滑动到的具体位置
     * @param position
     */
    public void setCurrentItem(int position){
        try {
            viewPager.setCurrentItem(position);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 模仿  listVIewAdapter 提供给外部使用
     * @param baseAdapter
     */
    public void setAdapter(T baseAdapter){
        viewAdapter.changeItmes(baseAdapter);
    }
    public class ViewAdapter extends PagerAdapter{
        private LinkedList<View> cacheList = new LinkedList<>();
        private T tAdapter;
        private ViewGroup parent;
        public ViewAdapter(ViewPager viewPager) {
            this.parent = viewPager;
        }

        public void changeItmes(T tAdapter){
            this.tAdapter = tAdapter;
            notifyDataSetChanged();//刷新
        }

        @Override
        public int getCount() {
            //此处返回 View的最大值
            return MAX_ZHi;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            super.destroyItem(container, position, object);
            View view = (View) object;
            //移动当前View
            container.removeView(view);
            cacheList.push(view); // 添加到第一个位置
//            cacheList.add(0, view); //具体要添加到什么位置
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            if (tAdapter == null || tAdapter.getCount() == 0){
                return null;
            }
            View view = null;
            if (cacheList.size() == 0 ){
                view = tAdapter.getView(position,null,parent);
            }else {
                View convertView = cacheList.get(cacheList.size() - 1);
                cacheList.remove(convertView);
                view = tAdapter.getView(position,convertView,parent);
            }
            return view;
        }
    }

    public interface T{
         int getCount();
         Object getItem(int position);
         View getView(int position,View view  , ViewGroup viewGroup);
    }
}
